package com.czkt.springboot_mybatis_test.controller;


import com.czkt.springboot_mybatis_test.entity.SysRight;
import com.czkt.springboot_mybatis_test.entity.SysRole;
import com.czkt.springboot_mybatis_test.service.ISysRightService;
import com.czkt.springboot_mybatis_test.service.ISysRoleRightService;
import com.czkt.springboot_mybatis_test.service.ISysRoleService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2024-06-18
 */
@Controller
@RequestMapping("/springboot_mybatis_test/sys-right")
public class SysRightController {
    @Resource
    ISysRoleRightService roleRightService;

    @Resource
    ISysRightService rightService;

    @Resource
    ISysRoleService roleService;

    @RequestMapping("/role/add")
    public String insertRoleRight(String roleId, Model model){
        List<SysRight> roleRights = roleRightService.roleRight(roleId);
        model.addAttribute("rights",rightService.list());
        model.addAttribute("roleRights",roleRights);
        return "/role/add";
    }

    @RequestMapping("/role/save")
    public String toInsertRoleRight(SysRole sysRole,String[] rightCodes){
        rightService.toInsertRoleRight(sysRole);
        SysRole sysRole1 = rightService.selectRoleId();
        for (int i = 0; i < rightCodes.length; i++) {
            roleRightService.insertRfRoleId(sysRole1.getRoleId(),rightCodes[i]);
        }
        return "redirect:/springboot_mybatis_test/sys-role/crm/role/list";
    }
}
